package com.example.administrator.foodrowfresh.jiao;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.foodrowfresh.R;
import com.example.administrator.foodrowfresh.classfile.JsonParserHomePage;
import com.example.administrator.foodrowfresh.jiao.modle.PromotionContentModel;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;

/**
 * 详情页
 */
public class PromotionContentActivity extends AppCompatActivity {

    //详情
    private ImageView image;
    private TextView name1, name2, name3, nameTV, last_time_infoTV, sx_priceTV, sp_weightTV, old_priceTV, old_price_weightTV, sellTV, typenameTV, descrptionTV;
    private TextView numberTV, standardTV, placeTV, storeTV;

    private PromotionContentModel.DataBean.ProductBean mProductBean;

    //第一层数据id
    private int totalCount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_promotion_content);
        //初始化shareSDK
        ShareSDK.initSDK(this);
        initView();
    }

    public void initView() {
        Bundle bundle = getIntent().getBundleExtra("bundle");
        String id = bundle.getString("id", "");

        //猜你喜欢
        String PATH = NetConfigPromotion.PROMOTION_ITEM_CONTENT_PATH_START + id + NetConfigPromotion.PROMOTION_ITEM_CONTENT_PATH_END;
        String LIKE_PATH = NetConfigPromotion.PROMOTION_CONTENT_LIKE_PATH_START + id + NetConfigPromotion.PROMOTION_CONTENT_LIKE_PATH_END;


        downloadDetails(PATH);
        downloadDTDetails(PATH);
        downloadnameDetails(PATH);

        //初始化
        image = (ImageView) findViewById(R.id.promotion_content_activity_image);
        nameTV = (TextView) findViewById(R.id.promotion_content_activity_tv_name);
        last_time_infoTV = (TextView) findViewById(R.id.promotion_content_activity_tv_last_time_info);
        sx_priceTV = (TextView) findViewById(R.id.promotion_content_activity_tv_sx_price);
        sp_weightTV = (TextView) findViewById(R.id.promotion_content_activity_tv_sp_weight);
        old_priceTV = (TextView) findViewById(R.id.promotion_content_activity_tv_old_price);
        old_price_weightTV = (TextView) findViewById(R.id.promotion_content_activity_tv_old_price_weight);
        sellTV = (TextView) findViewById(R.id.promotion_content_activity_tv_sell);

        typenameTV = (TextView) findViewById(R.id.promotion_content_activity_tv_TypeName);
        descrptionTV = (TextView) findViewById(R.id.promotion_content_activity_tv_Descrption);
        name1 = (TextView) findViewById(R.id.promotion_content_activity_bt_Name1);
        name2 = (TextView) findViewById(R.id.promotion_content_activity_bt_Name2);
        name3 = (TextView) findViewById(R.id.promotion_content_activity_bt_Name3);

        numberTV = (TextView) findViewById(R.id.promotion_content_activity_tv_number);
        standardTV = (TextView) findViewById(R.id.promotion_content_activity_tv_standardTV);
        placeTV = (TextView) findViewById(R.id.promotion_content_activity_tv_placeTV);
        storeTV = (TextView) findViewById(R.id.promotion_content_activity_tv_store);

    }


    // 下载详情页数据
    public void downloadDetails(String PATH) {
        RequestParams params = new RequestParams(PATH);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                mProductBean = JsonParserHomePage.parserPromotion(result);
                nameTV.setText(mProductBean.getName());
                last_time_infoTV.setText(mProductBean.getLast_time_info());
                sx_priceTV.setText(mProductBean.getSx_price());
                sp_weightTV.setText(mProductBean.getSp_weight());
                old_priceTV.setText(mProductBean.getOld_price());
                old_price_weightTV.setText(mProductBean.getOld_price_weight());
                sellTV.setText(mProductBean.getSell());

                numberTV.setText(mProductBean.getNumber());
                standardTV.setText(mProductBean.getStandard());
                placeTV.setText(mProductBean.getPlace());
                storeTV.setText(mProductBean.getStore());

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    //descrptionTV,typenameTV
    public void downloadDTDetails(String path) {
        RequestParams params = new RequestParams(path);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                List<PromotionContentModel.DataBean.PromotionalActivitiesBean> promotionalActivitiesBeans = JsonParserHomePage.promotionalActivitiesPromotion(result);
                for (int i = 0; i < promotionalActivitiesBeans.size(); i++) {
                    descrptionTV.setText(promotionalActivitiesBeans.get(i).getDescrption());
                    typenameTV.setText(promotionalActivitiesBeans.get(i).getTypeName());
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    //name
    public void downloadnameDetails(String path) {
        RequestParams params = new RequestParams(path);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                List<PromotionContentModel.DataBean.ProductserivesBean> productserivesBeans = JsonParserHomePage.namePromotion(result);
                for (int i = 0; i < productserivesBeans.size(); i++) {
                    if (Integer.parseInt(productserivesBeans.get(i).getId()) == 1) {
                        name1.setText(productserivesBeans.get(i).getName());
                    } else if (Integer.parseInt(productserivesBeans.get(i).getId()) == 2) {
                        name2.setText(productserivesBeans.get(i).getName());
                    } else if (Integer.parseInt(productserivesBeans.get(i).getId()) == 3) {
                        name3.setText(productserivesBeans.get(i).getName());
                    }

                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }


    public void onClick(View view) {
        if (view != null) {
            switch (view.getId()) {
                case R.id.promotion_item_activity_bt_share:
                    showShare(false, null);
                    break;
                case R.id.promotion_item_activity_bt_back:
                    finish();
                    break;
            }
        }
    }

    // 使用快捷分享完成分享
    private void showShare(boolean silent, String platform) {
        final OnekeyShare oks = new OnekeyShare();
    // 分享时Notification的图标和文字  2.5.9以后的版本不调用此方法
        //oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
        //分享标题
        oks.setTitle("#name#");
        oks.setTitleUrl("http://www.baidu.com");
        oks.setComment("....");
        //分享图片
        oks.setImageUrl("http://f1.sharesdk.cn/imgs/2014/05/21/oESpJ78_533x800.jpg");
        oks.setSilent(silent);
        if (platform != null) {
            oks.setPlatform(platform);
        }

        oks.show(this);
    }

    //如果activity被销毁，就停止shareSDK
    @Override
    protected void onDestroy() {
        super.onDestroy();
        ShareSDK.stopSDK(this);
    }
}
